package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.util.DateUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdInterfaceConfig;
import com.yunxi.otd.pojo.OtdInterfaceConfigQueryVO;
import com.yunxi.otd.pojo.OtdInterfaceConfigVO;
import com.yunxi.otd.service.OtdInterfaceConfigDubboService;
import com.yunxi.service.localService.otd.OtdInterfaceConfigService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * <p>
 * SO、CIR接口时间设置 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-10
 */
public class OtdInterfaceConfigDubboServiceImpl implements OtdInterfaceConfigDubboService {

    @Autowired
    @Qualifier(value = "otdInterfaceConfigServiceImpl")
    OtdInterfaceConfigService otdInterfaceConfigService;

    @Override
    public OtdInterfaceConfigVO getById(String id) {
        return convertVO(otdInterfaceConfigService.getById(id));
    }

    /**
     * 保存（新增/修改）
     *
     * @param configVO vo
     * @return ApiResult
     */
    @Override
    public ApiResult saveOrUpdate(OtdInterfaceConfigVO configVO) {
        OtdInterfaceConfig config = convertBean(configVO);
        config.setState("W");
        //执行时间不能早于当前时间
        if (new Date().after(config.getExecuteTime())) {
            ResponseUtil.fail(-1, "执行时间不能早于当前时间");
        }
        //需求开始日日期不能小于需求结束日期
        if (config.getStartDate().after(config.getEndDate())) {
            ResponseUtil.fail(-1, "需求开始日日期不能小于需求结束日期");
        }
        // 校验接口类型+执行时间是否重复
        LambdaQueryWrapper<OtdInterfaceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OtdInterfaceConfig::getType, config.getType())
            .eq(OtdInterfaceConfig::getExecuteTime, config.getExecuteTime());
        if (!StringUtil.isEmpty(config.getId())) {
            queryWrapper.ne(OtdInterfaceConfig::getId, config.getId());
        }
        int executeCount = otdInterfaceConfigService.count(queryWrapper);
        if (executeCount > 0) {
            return ResponseUtil
                .fail(-1, "该接口类型已设置过" + DateUtil.format(configVO.getExecuteTime(), "yyyy-MM-dd HH:mm") + " 的下发计划");
        }
        // SO/PSO 校验需求日期不能重复
        if(!"CIR".equals(config.getType())){
            // 校验下发需求日期周期不能重复
            LambdaQueryWrapper<OtdInterfaceConfig> checkWrapper = new LambdaQueryWrapper<>();
            checkWrapper.eq(OtdInterfaceConfig::getType, config.getType());
            checkWrapper.and(wrapper -> {
                wrapper
                    .or(wrapperOr -> wrapperOr.le(OtdInterfaceConfig::getStartDate, config.getStartDate())
                        .ge(OtdInterfaceConfig::getEndDate, config.getStartDate()))
                    .or(wrapperOr -> wrapperOr.le(OtdInterfaceConfig::getStartDate, config.getEndDate())
                        .ge(OtdInterfaceConfig::getEndDate, config.getEndDate()))
                    .or(wrapperOr1 -> wrapperOr1.ge(OtdInterfaceConfig::getStartDate, config.getStartDate())
                        .le(OtdInterfaceConfig::getStartDate, config.getEndDate()))
                    .or(wrapperOr2 -> wrapperOr2.ge(OtdInterfaceConfig::getEndDate, config.getStartDate())
                        .le(OtdInterfaceConfig::getEndDate, config.getEndDate()));
                return wrapper;
            });
            if (!StringUtil.isEmpty(config.getId())) {
                checkWrapper.ne(OtdInterfaceConfig::getId, config.getId());
            }
            int checkDemandDateCount = otdInterfaceConfigService.count(checkWrapper);
            if (checkDemandDateCount > 0) {
                return ResponseUtil
                    .fail(-1, "需求周期设置重复");
            }
        }
        otdInterfaceConfigService.saveOrUpdate(config);
        return ResponseUtil.ok();
    }

    /**
     * 分页查询
     *
     * @param queryVO vo
     * @param pageNum 页数
     * @param pageSize 行数
     * @return PageInfo
     */
    @Override
    public PageInfo findPageByVO(OtdInterfaceConfigQueryVO queryVO, Integer pageNum, Integer pageSize) {
        return otdInterfaceConfigService.pageByVo(queryVO, pageNum, pageSize);
    }

    /**
     * 查询全部
     *
     * @param otdInterfaceConfigVO VO
     * @return List
     */
    @Override
    public List listByVo(OtdInterfaceConfigVO otdInterfaceConfigVO) {
        return otdInterfaceConfigService.listByVo(otdInterfaceConfigVO);
    }

    @Override
    public void removeByIds(List<String> ids) {
        otdInterfaceConfigService.removeByIds(ids);
    }

    /**
     *  获取已发送的日期
     * @param yearMonth 年月
     * @return ApiResult
     */
    @Override
    public ApiResult getSendDays(String yearMonth, String type) {
        return ResponseUtil.ok(otdInterfaceConfigService.findAlreadySendDayList(yearMonth, type));
    }

    /**
     * VO to BEAN
     *
     * @param configVO VO
     * @return BEAN
     */
    private OtdInterfaceConfig convertBean(OtdInterfaceConfigVO configVO) {
        OtdInterfaceConfig config = new OtdInterfaceConfig();
        BeanUtils.copyProperties(configVO, config);
        return config;
    }

    /**
     * BEAN to VO
     *
     * @param config BEAN
     * @return VO
     */
    private OtdInterfaceConfigVO convertVO(OtdInterfaceConfig config) {
        OtdInterfaceConfigVO configVO = new OtdInterfaceConfigVO();
        if (config == null) {
            return null;
        }
        BeanUtils.copyProperties(config, configVO);
        return configVO;
    }



}
